<?php

session_start ();
$card;
if (isset ( $_SESSION ['card'] )) {
   $card = $_SESSION ['card'];
} else {
   $card = array ();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mobile Shop</title>

<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function ajax(id,count) {
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      
   	xmlhttp.open("GET","action/updatecard.php?id="+id+"&count="+count,true);
   	xmlhttp.send();
   	xmlhttp.onreadystatechange=function(){ 
       	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
           	document.getElementById("sp"+id).innerHTML=xmlhttp.responseText;
           	ajax1();
         } 
    }
	}
function ajax1(){
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

	xmlhttp.open("GET","action/sum.php",true);
	xmlhttp.send();
	xmlhttp.onreadystatechange=function()
	  { if (xmlhttp.readyState==4 && xmlhttp.status==200) {
	  document.getElementById("spsum").innerHTML=xmlhttp.responseText;
	  document.getElementById("spsum1").innerHTML=xmlhttp.responseText;
	 } }
}
</script>
</head>

<body>
	<div id="container">
      <?php include_once 'includes/header.php';?>
      <?php include_once 'includes/menu.php';?>
		<div id="center">
            <?php include_once 'includes/left.php';?>
			<div id="right">
				<table width="100%" border="1">

					<tbody>
					<?php
   $count = 1;
   $sum=0;
   foreach ($card as $sp ) {
   $sum+=$sp['price']*$sp['count'];
      ?>
						<tr>
							<td width="5%" align="center"><?php echo $count++;?></td>
							<td width="35%"><?php echo $sp['name'];?></td>
							<td width="20%" align="center" valign="middle"><img
								src="<?php echo $sp['image'];?>" width="100" height="100" alt="" /></td>
							<td width="10%" align="center"><label for="textfield"></label> <input
								name="textfield" type="text" id="textfield" size="5"
								value="<?php echo $sp['count'];?>"
								onkeyup="ajax(<?php echo $sp['id'] ?>, this.value)"  /></td>
							<td width="15%" align="right"><?php echo $sp['price']." VNĐ";?></td>
							<td width="15%" align="right"><span
								id="<?php echo "sp".$sp['id']?>"><?php echo ($sp['price']*$sp['count'])." VNĐ";?></span></td>
						</tr>
						<?php }?>
					</tbody>
					<thead>
						<tr align="center">
							<td width="5%" align="center">STT</td>
							<td width="35%">Tên Sản Phẩm</td>
							<td width="20%" align="center" valign="middle">Image</td>
							<td width="10%" align="center">Số Lượng</td>
							<td width="10%">Giá</td>
							<td width="15%">TỔNG:<span id="spsum"> <?php echo $sum." VNĐ";?></span></td>
						</tr>
					</thead>
					<tfoot>
						<tr align="center">
							<td width="5%" align="center">STT</td>
							<td width="35%">Tên Sản Phẩm</td>
							<td width="20%" align="center" valign="middle">Image</td>
							<td width="10%" align="center">Số Lượng</td>
							<td width="10%">Giá</td>
							<td width="15%">TỔNG:<span id="spsum1"> <?php echo $sum." VNĐ";?></span></td>
						</tr>
					</tfoot>
				</table>
			</div>


		</div>
      <?php include_once 'includes/footer.php';?>
	</div>
</body>
</html>
